// /recipel/infobyid
import request from "../../utils/request.ts";
import { message } from "antd";
import type { AxiosResponse } from "axios";

//获取处方开具
export const getPreReceiptsList = async (): Promise<any> => {
  try {
    const res: AxiosResponse<any, any> = await request.get("/drugs/list", {
      page: 1,
      size: 100,
    });
    if (res.data.code === 1) {
      return res.data;
    }
    message.error(res.data.msg);
  } catch (err) {
    message.error("获取首页处方开具失败");
    throw err;
  }
};
//查询
export const selectPreReceiptsList = async (prams: string): Promise<any> => {
  try {
    const res: AxiosResponse<any, any> = await request.get("/drugs/search", {
      prams,
    });
    if (res.data.code === 1) {
      return res.data;
    }
    message.error(res.data.msg);
  } catch (err) {
    message.error("获取首页处方开具失败");
    throw err;
  }
};

//删除药品
export const delPreReceiptsRow = async (prams: number): Promise<any> => {
  try {
    const res: AxiosResponse<any, any> = await request.get("/drugs/del", {
      id: prams,
    });
    if (res.data.code === 1) {
      return res.data;
    }
    message.error(res.data.msg);
  } catch (err) {
    message.error("删除处方开具失败");
    throw err;
  }
};
//删除批量药品
export const delPreReceiptsMore = async (prams: any): Promise<any> => {
  try {
    const res: AxiosResponse<any, any> = await request.post("/drugs/batchdel", {
      ids: prams,
    });
    if (res.data.code === 1) {
      return res.data;
    }
    message.error(res.data.msg);
  } catch (err) {
    message.error("删除处方开具失败");
    throw err;
  }
};
// 开具处方

export const AddPrescription = async (prams): Promise<any> => {
  try {
    const res: AxiosResponse<any, any> = await request.post(
      "/recipel/add",
      prams
    );
    if (res.data.code === 1) {
      return res.data;
    }
    message.error(res.data.msg);
  } catch (err) {
    message.error("添加处方开具失败");
    throw err;
  }
};
// 添加药品
export const AddMedicines = async (
  name: string,
  sort: string,
  type: string,
  starttime: string,
  endtime: string,
  count: number,
  price: number,
  img: string
): Promise<any> => {
  try {
    const res: AxiosResponse<any, any> = await request.post("/drugs/add", {
      name: name,
      sort: sort,
      type: type,
      starttime: starttime,
      endtime: endtime,
      count: count,
      price: price,
      img: img,
    });
    if (res.data.code === 1) {
      return res.data;
    }
    message.error(res.data.msg);
  } catch (err) {
    message.error("添加处方开具失败");
    throw err;
  }
};
// 上传图片
export const AddImg = async (): Promise<any> => {
  try {
    const res: AxiosResponse<any, any> = await request.post(
      "/drugs/uploaddrugs"
    );
    if (res.data.code === 1) {
      return res.data;
    }
    message.error(res.data.msg);
  } catch (err) {
    message.error("上传图片失败");
    throw err;
  }
};
